function OptsGen = OptsGenLoad(OptsGen)
%OptsGenLoad - interactive load of a StaVel options file
%
%   OptsGenOut = OptsGenLoad(OptsGen)
%
% If OptsGen is a struct variable to be used by StaVelMain and functions 
% called by it, it is simply OptsGenOut = OptsGen.
% If OptsGen is undefined or empty, the name of the file carrying such a 
% struct variable can be interactively managed. In this case, the file must
% have a field whose name is either Opts or OptsGen, which is returned as
% OptsGenOut. 
% If the dataload is unsuccessful, OptsGenOut = []. 
%
% See also StaVelMain, CMEstackFiltering, InspectOffsetSearch, showMom.

% G. Teza, 2022

if (nargin < 1) || ~isstruct(OptsGen)
    OptsGen = [];
end

if isempty(OptsGen)
    [filyg,pathyg] = uigetfile(...
        {'*.mat','(*.mat)'},...
        'STAVEL OPTIONS FILE (*.mat)');
    if isnumeric(filyg) || isnumeric(pathyg)
        filenag = []; 
    else
        filenag = fullfile(pathyg,filyg);
        try 
            S = load(filenag);
            if isfield(S,'Opts') 
                OptsGen = S.Opts;
            elseif isfield(S,'OptsGen')
                OptsGen = S.OptsGen;
            else
                filenag = [];
            end
        catch
            filenag = [];
        end
    end
    if isempty(filenag)
        OptsGen = [];
    end
end